/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * giaoca.java
 *
 * Created on Jun 13, 2012, 7:00:07 PM
 */
package t7;
import javax.swing.event.*;
import javax.swing.table.TableModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author trungdung
 */
public class giaoca extends javax.swing.JFrame {

    /** Creates new form giaoca */
    public giaoca(int userid) {
        initComponents();
        nvid=userid;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        luongngay = new javax.swing.JLabel();
        tongtien1 = new javax.swing.JLabel();
        luongdem = new javax.swing.JLabel();

        setTitle("Giao ca tầng 7");

        mainPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jTable1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Mỳ Tôm", null, null, null, null,  new Integer(7000), null},
                {"Bánh mỳ", null, null, null, null,  new Integer(3000), null},
                {"Trứng", null, null, null, null,  new Integer(5000), null},
                {"Sting dâu", null, null, null, null,  new Integer(7000), null},
                {"Sting vàng", null, null, null, null,  new Integer(7000), null},
                {"C2, BiDao", null, null, null, null,  new Integer(8000), null},
                {"Pepsi", null, null, null, null,  new Integer(6000), null},
                {"7UP,MIRI", null, null, null, null,  new Integer(6000), null},
                {"Aqua, Dasa", null, null, null, null,  new Integer(6000), null},
                {"Bò húc", null, null, null, null,  new Integer(13000), null},
                {"Vina bao", null, null, null, null,  new Integer(20000), null},
                {"Vina điếu", null, null, null, null,  new Integer(1000), null},
                {"Thăng long", null, null, null, null,  new Integer(11000), null},
                {"Malboro", null, null, null, null,  new Integer(25000), null},
                {"Café", null, null, null, null,  new Integer(5000), null},
                {"Xúc xích", null, null, null, null,  new Integer(10000), null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Tên Dịch Vụ", "Nhập thêm", "Tồn đầu", "Xuất", "Tồn cuối", "Giá", "Thành tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, false, true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowHeight(25);
        jTable1.setRowSelectionAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.setShowGrid(true);
        jTable1.setGridColor(java.awt.Color.BLUE);
        //jTable1.getModel().addTableModelListener(ln);
        jTable1.getModel().addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent e) {
                int row = e.getFirstRow();
                int column = e.getColumn();
                if((column!=1)&&(column!=3)&&(column!=4)) return;
                TableModel model = (TableModel)e.getSource();
                String columnName = model.getColumnName(column);
                Integer data = (Integer)model.getValueAt(row, column);
                //System.out.println(model.getValueAt(row, 2));

                //ton cuoi thay doi => xuat thay doi
                //System.out.println(model.getValueAt(row, 2));
                if(column==4)
                model.setValueAt(-unull(data)+unull(model.getValueAt(row, 2))+unull(model.getValueAt(row, 1)), row, 3);

                //xuat thay doi => Tien thay doi,Tong thay doi
                if(column==3) {
                    model.setValueAt(unull(data) * unull(model.getValueAt(row, 5)), row, 6);
                    //sum +=unull(data) * unull(model.getValueAt(row, 5));
                    int sum=0;
                    for(int i=0;i<model.getRowCount();i++)
                    {
                        sum+=unull(model.getValueAt(i, 6));
                    }

                    sum_global=sum;
                    // System.out.println(sum_global);

                    luongngay_int=sum*12/100;
                    luongdem_int=sum*14/100;

                    tongtien1.setText(" Tổng                     : "+Integer.toString(sum));
                    luongngay.setText(" Lương ngày : "+Integer.toString(luongngay_int));
                    luongdem.setText(" Lương đêm : "+Integer.toString(luongdem_int));
                }
                //Nhap thay doi => Xuat thay doi
                if(column==1)
                model.setValueAt(-unull(model.getValueAt(row, 4))+unull(model.getValueAt(row, 2))+unull(data), row, 3);
            }

        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
        );

        luongngay.setText(" Lương ngày   :");
        luongngay.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        tongtien1.setText(" Tổng thu            : ");
        tongtien1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        luongdem.setText(" Lương đêm   :");
        luongdem.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(811, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tongtien1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(luongngay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(luongdem, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(249, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(374, Short.MAX_VALUE)
                .addComponent(tongtien1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(luongngay, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(luongdem, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(95, 95, 95))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private Integer unull(Object obj) {
        if(obj!=null) return Integer.parseInt(obj.toString());
        else return 0;
    }
    
    private void resetTable() {
        for(int i=0;i<jTable1.getRowCount();i++)
            ((DefaultTableModel)jTable1.getModel()).removeRow(i);  
    }
    private String dau;
    private String cuoi;
    private int nvid;
    boolean stt_dalaydulieu=false;
    private int sum_global;
    int luongngay_int,luongdem_int;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel luongdem;
    private javax.swing.JLabel luongngay;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel tongtien1;
    // End of variables declaration//GEN-END:variables
    //private listener ln=new listener();
}
